package com.wonders.hisdoctor.station.project.examine.controller;

import com.wonders.his.common.bean.BaseResult;
import com.wonders.his.common.exception.CustomException;
import com.wonders.his.common.utils.StringUtils;
import com.wonders.his.common.utils.validator.ValidateUtils;
import com.wonders.his.common.utils.validator.group.SelectGroup;
import com.wonders.his.framework.dto.LoginUserDTO;
import com.wonders.his.framework.service.TokenService;
import com.wonders.hisdoctor.station.enums.ChargeIndicatorEnum;
import com.wonders.hisdoctor.station.project.base.domain.OutpTreatRec;
import com.wonders.hisdoctor.station.project.examine.service.IExamineService;
import com.wonders.hisdoctor.station.project.examine.vo.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.Date;
import java.util.List;

/**
 * Description: 检查申请
 *
 * @author zhanghaonan
 * <p>
 * Create: 2021-01-18 16:53
 */
@Api(tags = "检查申请")
@RestController
@RequestMapping("/examine")
public class ExamineController {

    @Autowired
    private IExamineService examineService;

    @Autowired
    private TokenService tokenService;

    @ApiOperation("查询检查项目左侧菜单")
    @GetMapping("/selectExamineMenu")
    public BaseResult<List<ExamineMenuVo>> selectCheckMenu(@RequestParam("hospitalCode") String hospitalCode) {
        return BaseResult.success(examineService.selectCheckMenu(hospitalCode));
    }

    @ApiOperation("查询检查项目")
    @PostMapping("/selectItemList")
    public BaseResult<List<ExamItemsVo>> selectItemList(@RequestBody ExamItemsRequestVo vo) {
        return BaseResult.success(examineService.selectItemList(vo));
    }

    @ApiOperation("提交申请(保存)")
    @PostMapping("/save")
    public BaseResult<Integer> save(@RequestBody ExamineSaveVo vo) {
        return BaseResult.success(examineService.save(vo));
    }

    @ApiOperation("保存并打印")
    @PostMapping("/saveAndPrint")
    public BaseResult<List<PrintExamineVo>> saveAndPrint(@RequestBody ExamineSaveVo vo) {
        return BaseResult.success(examineService.saveAndPrint(vo));
    }

    @ApiOperation("查询检查项目申请列表")
    @PostMapping("/selectOutpTreatRec")
    public BaseResult<List<OutpTreatRecVo>> selectOutpTreatRec(@RequestBody @Valid ExamineRequestVo vo) {
        return BaseResult.success(examineService.selectOutpTreatRec(vo));
    }

    @ApiOperation("删除检查申请项目")
    @PostMapping("/deleteOutpTreatRec")
    public BaseResult<Integer> deleteOutpTreatRec(@RequestBody List<OutpTreatRec> list) {
        return BaseResult.success(examineService.deleteOutpTreatRec(list));
    }

    @ApiOperation("查询历史检查申请")
    @PostMapping("/selectExamineHistory")
    public BaseResult<List<OutpTreatRecVo>> selectExamineHistory(@RequestBody ExamineHistoryRequestVo vo, HttpServletRequest request) {
        ValidateUtils.validateEntity(vo, new Class[]{SelectGroup.class});
        LoginUserDTO loginUserDTO = tokenService.getLoginUserDTO(tokenService.getToken(request));
        if (loginUserDTO != null && loginUserDTO.getSysSysuserVo() != null) {
            vo.setDoctor(loginUserDTO.getSysSysuserVo().getEmpNo());
        }
        //如果是查询已开单的历史申请，当前医生工号必传，为了排除当前登录医生的已开单申请。--历史申请不展示当前登录医生的已开单申请
        if(vo.getChargeIndicator() != null && ChargeIndicatorEnum.ORDERED.getCode() == vo.getChargeIndicator() && StringUtils.isEmpty(vo.getDoctor())){
            throw new CustomException("当前登录医生工号不能为空");
        }
        return BaseResult.success(examineService.selectExamineHistory(vo));
    }

    @ApiOperation("打印检查申请")
    @PostMapping("/selectPrintExamine")
    public BaseResult<List<PrintExamineVo>> selectPrintExamine(@RequestBody List<OutpTreatRecVo> list) {
        return BaseResult.success(examineService.selectPrintExamine(list));
    }
}
